// 获取mongoose
const mongoose = require("mongoose");

//get商品类型列表数据
const get = async ({
    curPage,
    eachPage
}) => {
    // 强制转换类型 number转string
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        total: 0,
        maxPage: 0,
        rows: [],
    }

    // 是获取数据库的自提点的的数据库
    const addressModel = mongoose.model("address");
    // 获取当前model映射的那个集合里存在的文档总数
    data.total = await addressModel.countDocuments();
    // Math.ceil往上取整
    data.maxPage = Math.ceil(data.total / eachPage);

    //查询数据库所有数据 跳过（当前页-1）*当前页显示数据  limit 查询多少条数据  sort 排序  execute 立即执行
    const skipNum = (curPage - 1) * eachPage;
    data.rows = await addressModel.find().skip(skipNum).limit(eachPage).sort({
        _id: -1
    }).exec();
    return data;

};
//新增自提点
const addAddress = async ({ name, Address, longitude, latitude, Cold_store, Phone, Open_hours }) => {
    //先获取到商品类型对应的数据库集合
    const addressModel = mongoose.model("address");
    //通过用户输入的商品类型名先查找 看是否存在
    const data = await addressModel.find({ name })
    //如果data有长度 证明此商品类型在数据库已经存在
    if (data.length) {
        return false
    } else {

        //否则，将此商品类型添加到数据库
        await addressModel.create(
            { name, Address, longitude, latitude, Cold_store, Phone, Open_hours }
        )
        const newData = await addressModel.findOne({ name });

        return newData
    }
}
//删除自提点
const deleteAddress = async (_id) => {
    // 获取model对象
    const addressModel = mongoose.model('address');
    // 删除
    return await addressModel.remove({ _id });
}

//修改 自提点
const amendAddress = async (newAddress) => {
    console.log(newAddress);
    //先获取到商品类型对应的数据库集合
    const addressModel = mongoose.model("address");
    // 修改数据   updateOne第一个参数必须是对象， 需要修改类型的id值 
    console.log(newAddress._id);
    await addressModel.updateOne({ _id: newAddress._id }, newAddress);

}

// query    查询条件    parmas  需要修改的数据 上传图片
const update = async (query, params) => {
    const { Image, _id } = params.$push
    console.log(Image, _id);
    const addressModel = mongoose.model('address');
    await addressModel.updateOne({ _id }, { Image });
    query._id = _id
    //通过名字查询这条数据，返回这条完整的数据
    return addressModel.findOne(query);
}

//删除ADDRESS商户图片
const delImgs = async ({
    _id,
    Image
}) => {
    const addressModel = mongoose.model("address");
    return await addressModel.findOneAndUpdate({ _id }, { Image: "" })

}


const findOne = async _id => {
    // model传一个参数时表示获取这个model对象
    const addressModel = mongoose.model("address");
    // 添加电影数据
    return await addressModel.findOne({
        _id
    });
}

const search = async ({ value }) => {
    const searchData = {
        rows: [],

    }
    // model传一个参数时表示获取这个model对象
    const addressModel = mongoose.model("address");
    //通过名字查询这条数据，返回这条完整的数据
    searchData.rows= addressModel.find({name:{ $regex:value, $options: 'i' }}).exec()
   return searchData.rows
}

//暴露接口 给Router
module.exports = {
    get,
    addAddress,
    deleteAddress,
    amendAddress,
    update,
    delImgs,
    findOne,
    search
}
